package CountDownLatch;

import java.util.concurrent.CountDownLatch;

public class Test2 {
    public static void main(String[] args) throws InterruptedException {
        //设置十名选手参赛跑步
        CountDownLatch latch = new CountDownLatch(10);
        //创建10个线程来执行跑步任务
        for(int i = 0; i < 10; i++){
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName() + ":开始跑步~");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + ":到达终点~");
                    //计数器--
                    latch.countDown();
                }
            });
            thread.start();
        }
        //await等待全部选手到达终点
        latch.await();
        System.out.println("比赛结束");
    }
}
